import { setGlobal } from '@swda/utils'

function Window_PasswordCommand() {
    this.initialize.apply(this, arguments);
}

Window_PasswordCommand.prototype = Object.create(Window_Command.prototype);
Window_PasswordCommand.prototype.constructor = Window_PasswordCommand;

Window_PasswordCommand.prototype.initialize = function(x, y) {
    Window_Command.prototype.initialize.call(this, x, y);
    this.opacity = 0;
	this.contentsOpacity = 0;
    this.createButtons();
};



Window_PasswordCommand.prototype.numVisibleRows = function() {
    return 5;
};

Window_PasswordCommand.prototype.maxCols = function() {
    return 1;
};

// 窗口尺寸（隐藏原始窗口）
Window_PasswordCommand.prototype.windowWidth = function() {
    return 0;
};

Window_PasswordCommand.prototype.windowHeight = function() {
    return 0;
};

Window_PasswordCommand.prototype.makeCommandList = function() {
    this.addCommand("绿色",   'green');
    this.addCommand("白色",   'white');
    this.addCommand("蓝色",   'blue');
    this.addCommand("黄色",   'yellow');
    this.addCommand("红色",   'red');
};


Window_PasswordCommand.prototype.createButtons = function() {
    const buttonWidth = 175;
    const buttonHeight = 87;
    this._buttons = [];
    for (let i = 0; i < this.numVisibleRows(); i++) {
        let bitmap = ImageManager.loadBitmap('img/system/UI/', 'ButtonPassword'+(i+1));
        let button = new Sprite_PasswordButton();
        button.bitmap = bitmap;
        button.setColdFrame(0, 0, buttonWidth, buttonHeight);
        button.setHotFrame(0, buttonHeight, buttonWidth, buttonHeight);
        button.setOnFrame(0, 0, buttonWidth, buttonHeight);
        button.setClickHandler(this.onPasswordOk.bind(this, i));
        button.x = 0;
        button.y = i * 115;
        button.visible = true;
        this._buttons.push(button);
        this.addChild(button);
    }
};


Window_PasswordCommand.prototype.onPasswordOk = function(i) {
    this.select(i);
    this.processOk();
};

Window_PasswordCommand.prototype.processCursorMove = function() {
    if (this.isCursorMovable()) {
        var lastIndex = this.index();
		// 更新图标
		if (TouchInput.movingdate < Input.date) {				//键盘输入的情况
            if (Input.isRepeated('down')) {
                this.cursorDown(Input.isTriggered('down'));
            }
            if (Input.isRepeated('up')) {
                this.cursorUp(Input.isTriggered('up'));
            }
		}
        // 键盘/鼠标/触屏时，更新按钮帧（光标选到第几个，第几个就切换为on）
        for (i = 0; i < this.numVisibleRows(); i++) {
            if (i == this._index){
                this._buttons[i].onFrame();
                this._buttons[i].selected();
            } else {
                this._buttons[i].updateFrame();
                this._buttons[i].deselected();
            }
        }	
		if (this.index() !== lastIndex) {
			SoundManager.playCursor();
		}
    }
};

setGlobal('Window_PasswordCommand', Window_PasswordCommand)